package com.drcoffee.drcoffeemall.service.impl;

import com.drcoffee.drcoffeemall.entry.InvoiceRecords;
import com.drcoffee.drcoffeemall.entry.Result;
import com.drcoffee.drcoffeemall.entry.User;
import com.drcoffee.drcoffeemall.mapper.*;
import com.drcoffee.drcoffeemall.service.InvoiceService;
import com.drcoffee.drcoffeemall.util.GenerateCodeUtil;
import com.drcoffee.drcoffeemall.util.SendWxWorlUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class InvoiceServiceImpl implements InvoiceService {

    private final Redisson redisson;   // 注入Redisson对象

    final
    RedisTemplate<String, Object> redisTemplate;

    final
    InvoiceMapper invoiceMapper;

    final
    OrderMapper orderMapper;

    final
    GenerateCodeUtil generateCodeUtil;

    final
    SendWxWorlUtil sendWxWorlUtil;

    public InvoiceServiceImpl(InvoiceMapper invoiceMapper, Redisson redisson, RedisTemplate<String, Object> redisTemplate, GenerateCodeUtil generateCodeUtil, OrderMapper orderMapper, SendWxWorlUtil sendWxWorlUtil) {
        this.redisson = redisson;
        this.redisTemplate = redisTemplate;
        this.invoiceMapper = invoiceMapper;
        this.generateCodeUtil = generateCodeUtil;
        this.orderMapper = orderMapper;
        this.sendWxWorlUtil = sendWxWorlUtil;
    }

    @Override
    @Transactional
    public Result applyForInvoice(String token, InvoiceRecords invoiceRecords) {
        User user = (User) redisTemplate.opsForValue().get(token);
        if (user == null) {
            throw new NullPointerException();
        }
        String invoice_code = generateCodeUtil.generate("IN");
        invoiceRecords.setCode(invoice_code);
        invoiceRecords.setUser_account(user.getAccount());
        BigDecimal orderTotal = orderMapper.getOrderTotal(invoiceRecords.getOrder_code());
        invoiceRecords.setAmount(orderTotal);
        Integer invoiceRecordsCount = invoiceMapper.getInvoiceExistByOrderCode(invoiceRecords.getOrder_code());
        if (invoiceRecordsCount != 0) {
            return new Result(2, "WARN", "Already exist", null);
        }
        invoiceRecords.setCurrent_step(1);
        invoiceRecords.setFile_path("");
        // 存储开票记录
        invoiceMapper.insertInvoiceRecords(invoiceRecords);
        // 发送提醒至企业微信
        sendWxWorlUtil.sendAppoint(9);
        orderMapper.setInvoiceStatusByCode(invoiceRecords.getOrder_code(), 1);
        return new Result(1, "SUCCESS", "Request successful", null);
    }

    @Override
    public Result getLastInvoice(String token, String order_code) {
        User user = (User) redisTemplate.opsForValue().get(token);
        if (user == null) {
            throw new NullPointerException();
        }
        Integer invoiceCount = invoiceMapper.getInvoiceExistByOrderCode(order_code);
        if (invoiceCount != 0) {
            return new Result(2, "WARN", "", null);
        }
        InvoiceRecords invoiceRecords = invoiceMapper.getLastInvoice(user.getAccount());
        return new Result(1, "SUCCESS", "", invoiceRecords);
    }

    @Override
    public Result getInvoicePathByCode(String order_code) {
        String file_path = invoiceMapper.getInvoicePathByCode(order_code);
        return new Result(1, "SUCCESS", "", file_path);
    }
}
